package com.inspectortime.domain.type;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.inspectortime.util.CalendarUtil;

public class Time implements Comparable {
	
	private int hour; //0-23
	private int minute; //0-59
	
	public Time(Date date) {
		this.setFrom(date);
	}
	
	public Time(Calendar calendar) {
		this.setFrom(calendar);
	}
	
	public void setFrom(Calendar calendar) {
		this.hour = calendar.get(Calendar.HOUR_OF_DAY);
		this.minute = calendar.get(Calendar.MINUTE);
	}
	
	public void setFrom(Date date) {
		Calendar calendar = Calendar.getInstance();
		this.setFrom(calendar);		
	}
	
	public Time(int hour, int minute) {
		this.hour = hour;
		this.minute = minute;
	}
	
	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public boolean before(Time otherTime) {
		if (this.getHour() < otherTime.getHour()) {
			return true;
		} else if (this.getHour() == otherTime.getHour()) {
			if (this.getMinute() < otherTime.getMinute()) {
				return true;
			}
		}
		return false;
	}
	
	public boolean after(Time otherTime) {
		if (this.getHour() > otherTime.getHour()) {
			return true;
		} else if (this.getHour() == otherTime.getHour()) {
			if (this.getMinute() > otherTime.getMinute()) {
				return true;
			}
		}
		return false;
	}
	
	public Calendar toCalendar() {
		Calendar calendar = Calendar.getInstance();
		CalendarUtil.removeDate(calendar);
		calendar.set(Calendar.HOUR_OF_DAY, this.getHour());
		calendar.set(Calendar.MINUTE, this.getMinute());
		
		return calendar;
	}
	
	public boolean isBetween(Time startTime, Time endTime) {
		
		if (this.after(startTime) && this.before(endTime)) {
			return true;
		}
		
//		if (this.hour > startTime.getHour() && this.hour < endTime.getHour()) {
//				return true;
//		} else if (this.hour == startTime.getHour() && this.minute >= startTime.getMinute() && this.hour < endTime.getHour()) {
//			return true;
//		} else if (this.hour == endTime.getHour() && this.minute < endTime.getMinute()) {
//			return true;
//		} 
//		
		return false;
	}
	
	public boolean equals(Object other) {
		Time otherTime = (Time) other;
		return (this.getHour() == otherTime.getHour() && this.getMinute() == otherTime.getMinute());
	}


	public int compareTo(Object other) {
		final int BEFORE = -1;
	    final int EQUAL = 0;
	    final int AFTER = 1;
		Time otherTime = (Time) other;
		
		if (this.getHour() < otherTime.getHour()) {
			return BEFORE;
		} else if (this.getHour() > otherTime.getHour()) {
			return AFTER;
		} else {
			if (this.getMinute() < otherTime.getMinute()) {
				return BEFORE;
			}
			if (this.getMinute() > otherTime.getMinute()) {
				return AFTER;
			}
		}
		
		return EQUAL;
	}
	
	public Date toDate() {
		Date date = new Date();
		date.setHours(this.hour);
		date.setMinutes(this.minute);
		date.setSeconds(0);
		return date;
	}
	
	public String toString() {
		Date date = new Date();
		date.setHours(this.hour);
		date.setMinutes(this.minute);
		
		SimpleDateFormat format = new SimpleDateFormat("h:mma");
		return format.format(date);
		
	}
	

}
